package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/10/15.
 * 修改订单地址
 */

public class UpdateLocation extends BaseActivity {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.tv_address)
    TextView tvAddress;


    private String mAddress;
    private int orderID;
    private int index;


    @Override
    protected int setLayout() {
        return R.layout.activity_updatelocation;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("修改定位");
        Bundle extras = getIntent().getExtras();
        int orderType = extras.getInt(Constant.ORDER_TYPE);
        orderID = extras.getInt(Constant.ORDER_ID);
        mAddress = extras.getString(Constant.ADDRESS);
        index = extras.getInt(Constant.INDEX);

        if (orderType == 2) {
            //住房订单
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.pic_house));
        } else if (orderType == 1) {
            //车辆订单
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.pic_car));
        }else if(orderType==4){
            //个人订单
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.geren));
        }else if(orderType==5){
            //企业税金
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.qiyeshuiijin));
        }

    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    @OnClick(R.id.tv_address)
    void clickAddress() {
        DialogUtil.addressPick(getContext(), new DialogUtil.AddressListener() {
            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                tvAddress.setText(address);
                tvAddress.setTextColor(getResources().getColor(R.color.black));
                mAddress = address;
            }
        });
    }

    @OnClick(R.id.tv_create)
    void onCreate() {
        ApiCreator.getApi().updateLocation(orderID, mAddress)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        RxBus.get().send(RxBusCode.UPDATE_LOCATION_SUCCESS, new StatusBean(index, mAddress));
                        ToastUtil.showToast("修改成功");
                        finish();
                    }
                });
    }

}
